<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    :title="actionStatus == 'edit' ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="20" :sm="24" :xs="24">
          <a-form-item label="盘点位置" v-bind="validateInfos.store_code">
            <a-select
              optionFilterProp="label"
              :options="mendian"
              allow-clear
              v-model:value="form.store_code"
              placeholder="请选择盘点位置"
              @change="mendianChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="盘点柜组">
            <a-select
              optionFilterProp="label"
              :options="guizu"
              allow-clear
              v-model:value="form.group_code"
              placeholder="请选择盘点柜组"
              @change="guizuChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="业务日期" v-bind="validateInfos.business_time">
            <a-date-picker
              class="ele-fluid"
              valueFormat="YYYY/MM/DD"
              placeholder="请选择业务日期"
              v-model:value="form.business_time"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="请选择盘点范围" />
          <a-form-item label="货品大类">
            <a-select
              optionFilterProp="label"
              :options="dalei"
              allow-clear
              v-model:value="form.broad_class_code"
              placeholder="请选择货品大类"
              @change="daleiChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="货品种类">
            <a-select
              optionFilterProp="label"
              :options="zhonglei"
              allow-clear
              v-model:value="form.subclass_code"
              placeholder="请选择货品种类"
              @change="zhongleiChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="货品款式">
            <a-select
              optionFilterProp="label"
              :options="kuanshi"
              allow-clear
              v-model:value="form.style_code"
              placeholder="请选择货品款式"
              @change="kuanshiChange"
              autocomplete="off"
            />
          </a-form-item>

          <a-form-item label="供应商名称">
            <a-select
              optionFilterProp="label"
              :options="gongyingshang"
              allow-clear
              v-model:value="form.supplier_code"
              placeholder="请选择供应商名称"
              @change="supplierChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="系列名称">
            <a-select
              optionFilterProp="label"
              :options="xilie"
              allow-clear
              v-model:value="form.series_code"
              placeholder="请选择系列名称"
              @change="xilieChange"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="宝石类别">
            <a-select
              optionFilterProp="label"
              :options="leibie"
              allow-clear
              v-model:value="form.gem_category_code"
              placeholder="请选择宝石类别"
              @change="leibieChange"
              autocomplete="off"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  import { Form } from 'ant-design-vue';
  import { emailReg, phoneReg, identityReg } from 'ele-admin-pro';
  import {
    get_store_list,
    get_store_group_list
  } from '@/api/jewelry/receipts_purchase';
  import { get_list2 } from '@/api/material/broad_class';
  import {
    get_subclass_list,
    get_style_list,
    get_series_list,
    get_gem_category_list,
    get_supplier_list
  } from '@/api/jewelry/receipts_purchase';
  import { check_add } from '@/api/jewelry/receipts_store_check';

  const { push } = useRouter();
  // 表格列配置
  const useForm = Form.useForm;

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑edit/新建add
    actionStatus: String
  });

  const emit = defineEmits(['done', 'update:visible']);

  // 提交状态
  const loading = ref(false);
  const mendian = ref([]);
  const guizu = ref([]);
  const dalei = ref([]);
  const zhonglei = ref([]);
  const kuanshi = ref([]);
  const xilie = ref([]);
  const leibie = ref([]);
  const gongyingshang = ref([]);

  onMounted(async () => {
    await get_store_list({ page: 1, limit: 9999 }).then((res) => {
      mendian.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
    await get_list2({ page: 1, limit: 9999 }).then((res) => {
      dalei.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
    await get_subclass_list({ page: 1, limit: 9999 }).then((res) => {
      zhonglei.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
    await get_series_list({ page: 1, limit: 9999 }).then((res) => {
      xilie.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
    await get_supplier_list({ page: 1, limit: 9999 }).then((res) => {
      gongyingshang.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
    await get_style_list({ page: 1, limit: 9999 }).then((res) => {
      kuanshi.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
    await get_gem_category_list({ page: 1, limit: 9999 }).then((res) => {
      leibie.value = res.list.map((item) => {
        return {
          value: String(item.code),
          label: item.name
        };
      });
    });
  });

  const getStoreGroup = (store_code) => {
    get_store_group_list({ page: 1, limit: 9999, store_code: store_code }).then(
      async (res) => {
        guizu.value = await res.list.map((item) => {
          return {
            value: String(item.code),
            label: item.name
          };
        });
      }
    );
  };
  /* 更新门店数据 */
  const mendianChange = (value) => {
    console.log('门店', value);
    form.store_code = value;
    getStoreGroup(value);
  };
  /* 更新柜组数据 */
  const guizuChange = (value) => {
    console.log('柜组', value);
    form.group_code = value;
  };
  /* 更新货品大类数据 */
  const daleiChange = (value) => {
    console.log('货品大类', value);
    form.broad_class_code = value;
  };
  /* 更新货品种类数据 */
  const zhongleiChange = (value) => {
    console.log('货品种类', value);
    form.subclass_code = value;
  };
  /* 更新款式数据 */
  const kuanshiChange = (value) => {
    console.log('款式', value);
    form.style_code = value;
  };
  /* 更新系列数据 */
  const xilieChange = (value) => {
    console.log('系列', value);
    form.series_code = value;
  };
  /* 更新类别数据 */
  const leibieChange = (value) => {
    console.log('类别', value);
    form.gem_category_code = value;
  };
  /* 更新供应商数据 */
  const supplierChange = (value) => {
    console.log('供应商', value);
    form.supplier_code = value;
  };

  // 表单数据
  const form = reactive({
    code: '',
    store_code: null,
    group_code: null,
    business_time: '',
    broad_class_code: null,
    subclass_code: null,
    style_code: null,
    supplier_code: null,
    series_code: null,
    gem_category_code: null
  });
  // 表单验证规则
  const rules = reactive({
    store_code: [
      {
        required: true,
        message: '请选择盘点位置',
        trigger: 'change'
      }
    ],
    business_time: [
      {
        required: true,
        message: '请选择业务日期',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        console.log('form', form);
        //emit('done', form);
        check_add(form).then((res) => {
          console.log('))))', res);
          message.success(res.msg);
          toDetail(res.data);
        });
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };

  const toDetail = (code) => {
    console.log('看详情');
    push({
      path: '/jewelry/receipts_store_check/detail',
      query: { code: code, form: 'add' }
    });
  };
</script>
<style scoped></style>
